/**
 * 
 */
package com.android.weibogo;

import com.android.weibogo.AsyncImageLoader.ImageCallback;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

/**
 * @author xiang.jianhua
 *
 */
public class TecentWeiboAdapter extends WeiboAdapter {
	private AsyncImageLoader asyncImageLoader;
	private Context context;

	/**
	 * @param wbList
	 */
	public TecentWeiboAdapter(Context ctx, ListView view) {
		super(view);
		this.context = ctx;
		asyncImageLoader = new AsyncImageLoader();
	}

	/* (non-Javadoc)
	 * @see android.widget.Adapter#getView(int, android.view.View, android.view.ViewGroup)
	 */
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		if (position < getWbList().size()) {
			convertView = LayoutInflater.from(context).inflate(
					R.layout.tecent_weibo_list, null);
			ImageView wbIcon = (ImageView) convertView
					.findViewById(R.id.wbicon);
			TextView wbText = (TextView) convertView.findViewById(R.id.wbtext);
			TextView wbType = (TextView) convertView.findViewById(R.id.wbtype);
			TextView wbTime = (TextView) convertView.findViewById(R.id.wbtime);
			TextView wbUser = (TextView) convertView.findViewById(R.id.wbuser);
			ImageView wbImage = (ImageView) convertView
					.findViewById(R.id.wbimage);
			LinearLayout wbSourceLayout = (LinearLayout) convertView
					.findViewById(R.id.wbsourcelayout);
			TextView wbSourceText = (TextView) convertView
					.findViewById(R.id.wbsourcetext);
			ImageView wbSourceImage = (ImageView) convertView
					.findViewById(R.id.wbsourceimage);
			TextView wbSourceTimeAndFromText = (TextView) convertView
					.findViewById(R.id.wbsourcetimeandfrom);
			TecentWeiboInfo wbInfo = (TecentWeiboInfo) getWbList()
					.get(position);
			if (wbInfo != null) {
				convertView.setTag(wbInfo.getId());
				wbUser.setText(wbInfo.getUserName());
				if (wbInfo.getTypeResId() == R.string.weibo_type_cast) {
					wbType.setText(context.getString(R.string.weibo_type_cast));
				}
				wbTime.setText(wbInfo.getTime());
				wbText.setText(wbInfo.getText(), TextView.BufferType.SPANNABLE);
				WeibogoUtility.textHighlight(wbText, '#', '#');
				WeibogoUtility.textHighlight(wbText, '@', ':');
				WeibogoUtility.textHighlight(wbText, '@', ' ');
				WeibogoUtility.textHighlight2(wbText, "http://", " ");
				if (wbInfo.getHaveImage()) {
					wbImage.setImageResource(R.drawable.imageprompticon);
				}
				Drawable cachedImage = asyncImageLoader.loadDrawable(wbInfo
						.getUserIcon(), "50", wbIcon, new ImageCallback() {

					@Override
					public void imageLoaded(Drawable imageDrawable,
							ImageView imageView, String imageUrl,
							View progressIndicator) {
						imageView.setImageDrawable(imageDrawable);
					}

				}, null);
				if (cachedImage != null) {
					wbIcon.setImageDrawable(cachedImage);
				} else {
					wbIcon
							.setImageResource(android.R.drawable.sym_def_app_icon);
				}
				if (wbInfo.isHasSource()) {
					// source weibo
					wbSourceLayout.setVisibility(View.VISIBLE);
					String textStr = wbInfo.getSource().getUserName().concat(
							":").concat(wbInfo.getSource().getText());
					wbSourceText.setText(textStr);
					if (wbInfo.getSource().getHaveImage()) {
						wbSourceImage
								.setImageResource(R.drawable.imageprompticon);
					}
					textStr = wbInfo.getSource().getTime().concat(" ").concat(
							wbInfo.getSource().getFrom());
					wbSourceTimeAndFromText.setText(textStr);
				}
			}
		} else if(position != 0){
			// show next page item
			LinearLayout layout = new LinearLayout(context);
			TextView textView = new TextView(context);
			LinearLayout.LayoutParams p = new LinearLayout.LayoutParams(
					LinearLayout.LayoutParams.FILL_PARENT,
					LinearLayout.LayoutParams.WRAP_CONTENT);
			p.setMargins(0, 5, 0, 5);
			layout.addView(textView, p);
			textView.setGravity(Gravity.CENTER);
			textView.setTextSize(18);
			textView.setTextColor(Color.BLUE);
			textView.setText(context.getString(R.string.list_next_page));
			convertView = layout;
		}
		return convertView;
	}
}
